/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const BaseJsonAdapter = require('../base/BaseJsonAdapter');
const iconv = require("iconv-lite");
const vm = require('vm');
const request = require('request-promise');
const logger = require('../../conf/logger');
const querystring = require("querystring")
const urlencode = require("urlencode")
const cheerio = require('cheerio');
/**
 * 北京互金网
 * @type {module.Bjp2p}
 */
module.exports = class TencentCaiJingJs extends BaseJsonAdapter{

    initWinEnv(){
        global.window = {
            innerWidth:1232,
            innerHeight:632,
            screenX:11,
            screenY:22,
            screen:{
                width:1280,
                height:800
            }
        };

        global.document = {
            documentElement:{
                clientWidth:1217,
                clientHeight:550
            },
            body:{
                clientWidth:1217,
                clientHeight:1467
            },
            _cookie: [],

            get cookie(){
                return this._cookie;
            },

            set cookie(val){
                this._cookie.push(val);
            }
        };
    }

    async doRequest(){
        this.initWinEnv();
        let body = await super.doRequest()
        let decodeBody = iconv.decode(body, this.encoding).toString();
        return decodeBody;
    }


    async reqJson(){
		const body = await this.doRequest();
		let $ = cheerio.load(body);
		let jsStr = $("#articleLiInHidden").html();
        jsStr = jsStr.replace(/<script>/g,'')
        jsStr = jsStr.replace(/<\/script>/g,'')
        vm.runInThisContext(jsStr);
        let data = ARTICLE_LIST;
        /*jsStr = jsStr.replace(/\n/g,'')
        jsStr = jsStr.replace(/\/\/列表数据/g,'')
        jsStr = jsStr.replace(/var ARTICLE_LIST=/g,'')
        jsStr = jsStr.replace(/  /g,'')
        jsStr = jsStr.replace(/'/g,'"')
        let data = JSON.parse(jsStr);*/
		return data;
	}
}
